package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.enums.OrderPayStatus;
import com.runvp.common.utils.ShiroUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.biz.domain.BizIndexTj;
import com.runvp.service.biz.handler.TjHandler;
import com.runvp.service.biz.service.IBizDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 设备Controller
 *
 * @author runvp
 * @date 2022-09-18
 */
@Controller
@RequestMapping("/biz/tj")
public class BizTjController extends BaseController {

    @Autowired
    private IBizDeviceService bizDeviceService;

    @Autowired
    private TjHandler tjHandler;

    /**
     * 汇总
     */
    @Log(title = "汇总", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/all")
    public AjaxResult all(BizIndexTj query) {
        query.setTenantId(ShiroUtils.bizUserId());
        return AjaxResult.success(tjHandler.all(query));
    }

    /**
     * 查询设备列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizIndexTj query) {
        startPage();
        BizDevice device = new BizDevice();
        device.setTenantId(getUserIdFilterAdmin());
        List<BizDevice> list = bizDeviceService.selectBizDeviceList(device);
        TableDataInfo dataInfo = getDataTable(list);
        dataInfo.setRows(tjHandler.buildVOS(list,query));
        return dataInfo;
    }
    /**
     * 导出设备列表
     */
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizDevice bizDevice) {
        List<BizDevice> list = bizDeviceService.selectBizDeviceList(bizDevice);
        ExcelUtil<BizDevice> util = new ExcelUtil<BizDevice>(BizDevice.class);
        return util.exportExcel(list, "设备数据");
    }

    /**
     * 营收
     */
//    @Log(title = "营收统计", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/revenue")
    public AjaxResult revenue(BizIndexTj query) {
        if (query.getTenantId() == null) {
            query.setTenantId(ShiroUtils.bizUserId());
        }
        return AjaxResult.success(tjHandler.revenue(query));
    }

    /**
     * 趋势统计
     */
//    @Log(title = "趋势统计", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/trend")
    public AjaxResult trend(BizIndexTj query) {
        if (query.getTenantId() == null) {
            query.setTenantId(ShiroUtils.bizUserId());
        }
        return AjaxResult.success(tjHandler.trend(query));
    }

    /**
     * 近7日订单走势
     */
//    @Log(title = "近7日订单走势", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/near7Days")
    public AjaxResult near7Days(BizIndexTj query) {
        if (query.getTenantId() == null) {
            query.setTenantId(ShiroUtils.bizUserId());
        }
        query.setDays(7);
        return AjaxResult.success(tjHandler.nearDays(query));
    }

    /**
     * 今日新增用户数
     */
//    @Log(title = "今日新增用户数", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/nowdayNewAddUser")
    public AjaxResult nowdayNewAddUser() {
        return AjaxResult.success(tjHandler.nowdayNewAddUser());
    }

    /**
     * 近2日订单营收
     */
//    @Log(title = "近2日订单走势", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/near2Days")
    public AjaxResult near2Days(BizIndexTj query) {
        if (query.getTenantId() == null) {
            query.setTenantId(ShiroUtils.bizUserId());
        }
        query.setDays(2);
        return AjaxResult.success(tjHandler.near2Days(query));
    }
    /**
     * 今日设备在线情况
     */
//    @Log(title = "今日设备在线情况", businessType = BusinessType.OTHER)
    @ResponseBody
    @PostMapping("/nowdayDeviceOnLine")
    public AjaxResult nowdayDeviceOnLine(BizIndexTj query) {
        if (query.getTenantId() == null) {
            query.setTenantId(ShiroUtils.bizUserId());
        }
        return AjaxResult.success(tjHandler.nowdayDeviceOnLine(query));
    }


    @GetMapping("/order")
    public String order(String sn, String type, Long tenantId, String startTime, String endTime, ModelMap mmap) {
        mmap.put("sn", sn);
        mmap.put("typeV", type);
        mmap.put("tenantId", tenantId);
        mmap.put("startTimeV", startTime);
        mmap.put("endTimeV", endTime);
        mmap.put("payStatusV", OrderPayStatus.PAY_SUCCESS.getCode());
        return "biz/order/order";
    }

}
